/**
 * Copyright (c) 2011-2014, hubin (243194995@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.kisso.my.controller;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.SSOToken;

/**
 * <p>
 * 
 * @author hubin
 * @date 2015年3月25日
 * @version 1.0.0
 */
@Controller
public class OkLoginController extends BaseController {

	/**
	 * 跨域登录成功
	 */
	@ResponseBody
	@RequestMapping("/oklogin")
	public void oklogin() {
		String returl = "http://my.web.com:8090/timeout.html";
		/**
		 * <p>
		 * 回复密文是否存在
		 * </p>
		 * <p>
		 * SSO 公钥验证回复密文是否正确
		 * </p>
		 * <p>
		 * 设置 MY 系统自己的 Cookie
		 * </p>
		 */
		String replyTxt = request.getParameter("replyTxt");
		if (replyTxt != null && !"".equals(replyTxt)) {
			String userId = SSOHelper.ok(request, response, replyTxt,
					"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDKeDkRea/pgX2K6V/eOCQ8kImiqH+QkONx+ubVkvME9mr0I4lElNr8Hl1Q2zEQ+zz4zgDyOWoiCBxSwagBPTate15aMs+uplJL74ScW5gduBYcoQfHURC/ORCFGoa5Y6049p1lhow31yKkKGNTYQYupdlR4HFbqESOPFMwFnG0gQIDAQAB",
					"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDZmAZgJcQV0XjBOk/CB2nR+AXXyVMdcErLgz5LYb/g/Ar7tiHhYlGk69/mlItDnvHxeV/t7ibEqwvQnlDiM6BsYW/9HBzYWiF54D7hxd2MUWqNit232pS5XlmzurrFmqhSomrR0KKJHoA3HIsIGJ/AT9xzDb93GqPtDm6Yt24fwIDAQAB");
			if ("10001".equals(userId)) {
				returl = "http://my.web.com:8090/index.html";
				SSOToken st = new SSOToken();
				st.setUserId(10001L);
				/*
				 * authSSOCookie 设置 cookie 同时改变 jsessionId
				 */
				SSOHelper.authSSOCookie(request, response, st);

				/*
				 * 只设置 cookie SSOHelper.setSSOCookie(req, response, st);
				 */
			}
		}
		try {
			AjaxHelper.outPrint(response, "{\"returl\":\"" + returl + "\"}", "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
